<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="common::common(title='基础数据')">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" ></meta>
</head>
<link th:href="@{/plugins/file-upload/css/jquery.fileupload.css}" rel="stylesheet" />
<script type="text/javascript" th:src="@{/plugins/file-upload/js/vendor/jquery.ui.widget.js}"></script>
<script type="text/javascript" th:src="@{/plugins/file-upload/js/jquery.iframe-transport.js}"></script>
<script type="text/javascript" th:src="@{/plugins/file-upload/js/jquery.fileupload.js}"></script>
<script type="text/javascript" th:src="@{/plugins/file-upload/js/cors/jquery.xdr-transport.js}"></script>
<script th:inline="javascript">
var parentCode=[[${parentCode}]];

$(function(){
	
	$('#fileupload').fileupload({
		url : [[@{/basic/uploadFileAction/saveFile.do?type=0&location=A0101}]],//1:本地;0:远程
		dataType : 'json',
		autoUpload : true,
		singleFileUploads:true,
		multiple:false,
		add: function (e, data) {
			
			if(data.files.length>0){
				if(!$.uploadUtil.validate(data.files[0].name)) return false;
			}

            if (e.isDefaultPrevented()) return false;

            if(data.autoUpload||(data.autoUpload !== false&&$(this).fileupload('option', 'autoUpload'))){
            	
             	data.process().done(function () {
               		$.uploadUtil.process('#files',true);
                    data.submit();
             	});
            }
        },
		done : function(e, data) {
			
			if (!data.result.success) return false;
			
			//http协议浏览
			$.uploadUtil.http('#files',data.result.obj);
			
			
		}
	});
	
	if('2'==[[${opts}]]){//修改
		$.ajax({
			url :[[@{showDesc.do}]],
			data : {
				id : [[${id}]]
			},
			dataType : 'json',
			cache : false,
			success : function(json) {
				
				$('#baseseInfoForm').form('load', json);
                
				//禁用
				$('#baseseInfoForm').find("input[ name='itemName']").attr('disabled',true);
				$('#baseseInfoForm').find("input[ name='itemCode']").attr('disabled',true);
				//$('#baseseInfoForm').find("input[ name='itemCodeCopy']").attr('disabled',true);
				$('#parentCode').combotree('disable');
				
				var file = {};
				file.id=json.fileId;
				file.saveFileName=json.fileName;
				file.filePath = json.filePath;
				
				if(json.fileId)
					$.uploadUtil.add('#files',file);
			}
		});
	
		$('#baseseInfoForm').attr('action',[[@{edit.do}]]);
	}else if('1'==[[${opts}]]){//增加
		
		if([[${parentCode}]])
			$('#parentCode').combotree('setValue',[[${parentCode}]]);
	
		$('#baseseInfoForm').attr('action',[[@{add.do}]]);
	}
});
/**
 * 保存
 */
function save(){
	
	if(!$('#baseseInfoForm').form('validate'))
		return ;
	
	//图片信息
	$("input[name='fileId']",'#baseseInfoForm')[0].value='';
	$("input[name='fileName']",'#baseseInfoForm')[0].value=null;
	$("input[name='filePath']",'#baseseInfoForm')[0].value=null;
	
	var fileInfo = $.uploadUtil.findAll('#files');
	
	if(fileInfo&&fileInfo.length>0){
		$("input[name='fileId']",'#baseseInfoForm')[0].value=fileInfo[0].fileId;
		$("input[name='fileName']",'#baseseInfoForm')[0].value=fileInfo[0].fileName;
		$("input[name='filePath']",'#baseseInfoForm')[0].value=fileInfo[0].pathName;
	}
	
		
	 $('#baseseInfoForm').form('submit',{
		 onSubmit: function(){
                return $(this).form('validate');
            },
		 success:function(r){
			 try{
	      	 	var json = $.parseJSON(r);
			 
			    if (json.success)
				  $.subpage.refresh();
			   else
				  $.messager.alert('错误', json.msg, 'error'); 
			    
	      	   }catch(e){  
	       		  $.messager.show({title : '提示',msg : '操作失败！'});
	      	   }
		 }
	 });
}	
function selectedInfo(item){//设置编码
	
    var obj = item.obj,
    	parentName = obj.itemName,
    	parentCode = obj.itemCode,
    	childrenCount = item.children.length,
    	fillCode;
    
	if(childrenCount>0){ 
    	
    	var next = item.children;
    	
    	var nextCode = next[next.length-1].id;
    	
    	var lastCode = parseInt(nextCode.substring(nextCode.length-2));
    	
    	if(lastCode>8)
    		fillCode = lastCode+1;
    	else fillCode = '0'+parseInt(lastCode+1);
    	
    }
    else fillCode = '0'+parseInt(childrenCount+1);
    
	itemCode = parentCode+fillCode;
    
    $("#baseseInfoForm").find("input[ name='parentName']")[0].value=parentName;
    
    $($("#baseseInfoForm").find("input[ name='itemCode']")[0]).val(itemCode);
    
    $('#orderNum').numberspinner('setValue',parseInt(childrenCount)); 

}
function loadTree(){
	
	if('1'==[[${opts}]]&&parentCode){
		
		var combo = $('#parentCode').combotree('tree'),
		    item = combo.tree('find',parentCode);
		
		selectedInfo(item);
	}
}
</script>
<body>
	<div class="zoc">
		<div class="datagrid-toolbar">
			<a href="javascript:$.subpage.close(0);" class="l-btn l-btn-small l-btn-plain">
				<span class="l-btn-left l-btn-icon-left"> 
					<span class="l-btn-text">返回</span> <span class="l-btn-icon icon-back"></span>
			    </span>
			</a> 
			<a href="javascript:save();" class="l-btn l-btn-small l-btn-plain">
				<span class="l-btn-left l-btn-icon-left"> 
					<span class="l-btn-text">保存</span> <span class="l-btn-icon icon-save"></span>
			    </span>
			</a>
		</div>
		<div class="part_zoc">
			<form id="baseseInfoForm" th:action=@{edit.do} enctype="multipart/form-data"  method="post">
				<input type="hidden" name="parentName">
				<input type="hidden" name="id">
				
				<input type="hidden" name="fileId">
				<input type="hidden" name="fileName">
				<input type="hidden" name="filePath">
				
				<div class="oneline">
					<div class="item33">
						<div class="itemleft">父类节点<span style="color:red;">&nbsp;*</span></div>
						<div class="righttext">
							<select style="width:100%;" name="parentCode" id="parentCode"  class="easyui-combotree" 
								th:url=@{findTrees.do}
								data-options="animate: true,
											  checkbox:false,
											  onSelect:selectedInfo,
											  onLoadSuccess:loadTree" missingMessage="请选择父类节点"></select>
						</div>
					</div>
					<div class="item33">
						<div class="itemleft">名称<span style="color:red;">&nbsp;*</span></div>
						<div class="righttext">
							<input name="itemName" type="text" class="easyui-validatebox"
								data-options="required:true" missingMessage="请填写名称" />
						</div>
					</div>
					
				</div>
				<div class="oneline">
					<div class="item33">
						<div class="itemleft">编码<span style="color:red;">&nbsp;*</span></div>
						<div class="righttext">
							<input name="itemCode" type="text" class="easyui-validatebox"
								data-options="required:true" missingMessage="请填写编码" />
						</div>
					</div>
					<div class="item33">
						<div class="itemleft">序号<span style="color:red;">&nbsp;*</span></div>
						<div class="righttext">
							<input name="orderNum" id="orderNum" type="text" class="easyui-numberspinner"
								data-options="min:0,required:true" value="0" style="width:100%"/>
						</div>
					</div>
				</div>
				<div class="oneline">
					<div class="item33">
						<div class="itemleft">辅助参数</div>
						<div class="righttext">
							<input name="itemCodeCopy" type="text" class="easyui-validatebox" />
						</div>
					</div>
					<div class="item33">
						<div>
							<div class="fileinput-button" style="width: 100px; clear: both;">	
								<a href="javascript:void(0);" class="easyui-linkbutton" data-options="toggle:true,iconCls:'icon-upload'">图片</a>	
								<input id="fileupload" type="file" name="file">
							</div>
						</div>
					</div>
				</div>
				<div class="oneline">
					<div class="item100">
						<div class="itemleft" style="width:10%">备注</div>
						<div class="righttext" style="width:53%;">
							<input name="remark" type="text"  />					
						</div>
					</div>
				</div>
				<div class="oneline">
					<div class="fileList" id="files"></div>
				</div>
			</form>
		</div>
	</div>
</body>
</html>